from decimal import Decimal
from apps.api.models import User, UserRecord
from exit import db
import random
from datetime import datetime, timedelta, date
from chinese_calendar import is_workday


def cms_record_status(r_label, r_type):
    status = ''
    r_label = int(r_label)
    r_type = int(r_type)
    # print(r_label, r_type)
    if r_label == 1:
        if r_type == 1:
            status = '添加轮播'
        elif r_type == 2:
            status = '更新轮播'
        elif r_type == 3:
            status = '删除轮播'
    elif r_label == 2:
        if r_type == 1:
            status = '修改交易时间'
        elif r_type == 2:
            status = '暂停交易'
        elif r_type == 5:
            status = '开启交易'
        elif r_type == 3:
            status = '修改'
        elif r_type == 4:
            status = '修改付款账户'
    elif r_label == 3:
        if r_type == 1:
            status = '封号'
        elif r_type == 2:
            status = '解封'
        elif r_type == 3:
            status = '修改信息'
    elif r_label == 4:
        if r_type == 1:
            status = '积分兑换发货'
        elif r_type == 3:
            status = '完成提现订单'
        elif r_type == 4:
            status = '取消提现订单'
    elif r_label == 5:
        if r_type == 1:
            status = '完成购买订单'
        elif r_type == 2:
            status = '取消购买订单'
    elif r_label == 10:
        if r_type == 1:
            status = '修改密码'
    return status


def random_num(num):  # 随机数字
    order_num = ""
    for i in range(num):
        number = random.randint(0, 9)
        order_num += str(number)
    return order_num


def trade_no_time():    # 订单号(时间) +  随机数
    now = datetime.now().strftime('%Y%m%d%H%M%S')
    trade_no = now + random_num(6)
    return trade_no


def decimal_format(nums):   # 有一位小数就进一位
    if len(str(nums).split('.')[1]) >= 1:
        num = Decimal(str(nums).split('.')[0])
        num = int(num) + 1
    else:
        num = nums
    num = int(num)
    return num


def remove_exponent(num):   # 删除decimal类型的零
    return num.to_integral() if num == num.to_integral() else num.normalize()


def check_bank_card(card_num):
    """检查银行卡的合法性"""
    total = 0
    even = True
    if isinstance(card_num, int):
        card_num = str(card_num)
    check_num = card_num[-1]
    for item in card_num[-2::-1]:
        item = int(item)
        if even:
            item <<= 1
        if item > 9:
            item -= 9
        total += item
        even = not even
    return int(check_num) is (10 - (total % 10)) % 10


def get_date(num):  # 获取日期
    now = datetime.today() + timedelta(days=num)
    return now


def work_day(date_day):
    if is_workday(date_day):
        # print("是工作日")
        return True
    else:
        # print("是休息日")
        return False



